/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.railhunter.ui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import org.freedom.railhunter.model.Passenger;
import org.freedom.railhunter.ui.component.CheckBoxList;
import org.freedom.railhunter.ui.component.CheckBoxListEntry;

/**
 *
 * @author Alan
 */
public class PassengersSeleter extends javax.swing.JDialog {
    
    List<Passenger> selectedPassengerList = new ArrayList<>();

    /**
     * Creates new form PassengersSeleter
     */
    public PassengersSeleter(java.awt.Frame parent, boolean modal, Passenger[] passengers) {
        super(parent, modal);
        this.setTitle("选择乘车人");
        initComponents();
        DefaultListModel model = (DefaultListModel) passengersListComponent.getModel();
        for (Passenger passenger : passengers) {
            CheckBoxListEntry entry = new CheckBoxListEntry(passenger, false);
            model.addElement(entry);
        }
    }

    public List<Passenger> getSelectedPassengerList() {
        return selectedPassengerList;
    }

    public void setSelectedPassengerList(List<Passenger> selectedPassengerList) {
        this.selectedPassengerList = selectedPassengerList;
    }
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        passengersListComponent = new CheckBoxList();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setViewportView(passengersListComponent);

        cancelButton.setText("取消");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("确定");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 115, Short.MAX_VALUE)
                        .addComponent(okButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        List selectItems = ((CheckBoxList)passengersListComponent).getCheckedItems();
        for (Object item : selectItems) {
            if (item instanceof CheckBoxListEntry) {
                CheckBoxListEntry entry = (CheckBoxListEntry)item;
                selectedPassengerList.add((Passenger) entry.getValue());
            }
        }
        this.setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JList passengersListComponent;
    // End of variables declaration//GEN-END:variables
}
